#!/usr/bin/env python
# -*- coding:utf-8 -*-

import os.path

## 自动化配置Python对象文件脚本
import json
# from bean.WebSite import WebSite
# from bean.Catalog import Catalog
from bean.Article import Article
from bean.Questionnaire import Questionnaire
from exut import MakeParamBeanTools, MakeHandleTools, MakeServiceTools

# _json_file_name = "Website.json"
# _json_file_name = "Catalog.json"
_json_file_name = "Questionnaire.json"


@staticmethod
def step_three():
    try:
        _base_path = os.path.abspath("")
        sample_json_file = _base_path + "\\" + "json" + "\\" + _json_file_name
        with open(sample_json_file, 'r', encoding='utf-8') as file:
            data = json.load(file)

        # STEP 1 根据JSON对象生成需要的Bean文件
        bean_file_path = _base_path + "\\" + "handle"
        target_file = bean_file_path + "\\" + data['#serviceName#'] + ".py"
        MakeServiceTools.excute(_base_path, target_file, json_data=data)
    except Exception as err:
        print(err)


@staticmethod
def step_two():
    try:
        _base_path = os.path.abspath("")
        sample_json_file = _base_path + "\\" + "json" + "\\" + _json_file_name
        with open(sample_json_file, 'r', encoding='utf-8') as file:
            data = json.load(file)

        # STEP 1 根据JSON对象生成需要的Bean文件
        bean_file_path = _base_path + "\\" + "handle"
        target_file = bean_file_path + "\\" + data['#handleName#'] + ".py"
        MakeHandleTools.excute(_base_path, target_file, json_data=data)
    except Exception as err:
        print(err)


@staticmethod
def step_one():
    try:
        _base_path = os.path.abspath("")
        sample_json_file = _base_path + "\\" + "json" + "\\" + _json_file_name
        with open(sample_json_file, 'r', encoding='utf-8') as file:
            data = json.load(file)

        # STEP 1 根据JSON对象生成需要的Bean文件
        bean_file_path = _base_path + "\\" + "bean"
        target_file = bean_file_path + "\\" + data['#beanName#'] + ".py"

        # STEP 1.1 执行生成Bean对象内容
        # 类内属性处理
        attr_ary = []
        val_ary = []
        clas_name = Questionnaire.__name__
        for key, value in Questionnaire.__dict__.items():
            if str(key).startswith("__"):
                continue
            attr_ary.append(key)
            val_ary.append(value)
        print("初始化配置对象基础信息\n")
        print("读取JSON化对象文件内容: \n %s" % data)
        print("读取类对象基础结构: \n %s" % ','.join(attr_ary))
        print("对象容器路径: \n %s" % target_file)
        MakeParamBeanTools.make_bean_base_obj(data, attr_ary, val_ary, target_file)
        print("拼接类对象方法1: %s" % "split_data_clum")
        MakeParamBeanTools.make_split_data_clum(clas_name, target_file=target_file)
        print("\n")
        print("拼接类对象方法2: %s" % "split_params")
        MakeParamBeanTools.make_split_params(attr_ary, target_file=target_file)
        print("\n")
        print("拼接类对象方法3: %s" % "to_string")
        MakeParamBeanTools.make_to_string(attr_ary, clas_name, target_file=target_file)
        print("\n")
        print("拼接类对象方法4: %s" % "to_string_noId")
        MakeParamBeanTools.make_to_string_noId(attr_ary, clas_name, target_file=target_file)
        print("\n")
        print("拼接类对象方法5: %s" % "chg_data_2_dict")
        MakeParamBeanTools.make_chg_data_2_dict(clas_name, target_file=target_file)
        print("\n")
        print("拼接类对象方法6: %s" % "chg_dict_2_data")
        MakeParamBeanTools.make_chg_dict_2_data(attr_ary, target_file=target_file)
    except Exception as err:
        print(err)


if __name__ == '__main__':
    step_one()
    step_two()
    step_three()
